---
layout: "default"
title: "AnyObject"
description: "Swift documentation for 'AnyObject': The protocol to which all classes implicitly conform."
keywords: "AnyObject,protocol,swift,documentation"
root: "/v3.0"
---

<div class="intro-declaration"><code class="language-swift">protocol AnyObject</code></div>

<div class="discussion comment">
    <p>The protocol to which all classes implicitly conform.</p>

<p>You use <code>AnyObject</code> when you need the flexibility of an untyped object or
when you use bridged Objective-C methods and properties that return an
untyped result. <code>AnyObject</code> can be used as the concrete type for an
instance of any class, class type, or class-only protocol. For example:</p>

<pre><code class="language-swift">class FloatRef {
    let value: Float
    init(_ value: Float) {
        self.value = value
    }
}

let x = FloatRef(2.3)
let y: AnyObject = x
let z: AnyObject = FloatRef.self</code></pre>

<p><code>AnyObject</code> can also be used as the concrete type for an instance of a type
that bridges to an Objective-C class. Many value types in Swift bridge to
Objective-C counterparts, like <code>String</code> and <code>Int</code>.</p>

<pre><code class="language-swift">let s: AnyObject = &quot;This is a bridged string.&quot; as NSString
print(s is NSString)
// Prints &quot;true&quot;

let v: AnyObject = 100 as NSNumber
print(type(of: v))
// Prints &quot;__NSCFNumber&quot;</code></pre>

<p>The flexible behavior of the <code>AnyObject</code> protocol is similar to
Objective-C&#39;s <code>id</code> type. For this reason, imported Objective-C types
frequently use <code>AnyObject</code> as the type for properties, method parameters,
and return values.</p>

<h1>Casting AnyObject Instances to a Known Type</h1>

<p>Objects with a concrete type of <code>AnyObject</code> maintain a specific dynamic
type and can be cast to that type using one of the type-cast operators
(<code>as</code>, <code>as?</code>, or <code>as!</code>).</p>

<p>This example uses the conditional downcast operator (<code>as?</code>) to
conditionally cast the <code>s</code> constant declared above to an instance of
Swift&#39;s <code>String</code> type.</p>

<pre><code class="language-swift">if let message = s as? String {
    print(&quot;Succesful cast to String: \(message)&quot;)
}
// Prints &quot;Succesful cast to String: This is a bridged string.&quot;</code></pre>

<p>If you have prior knowledge that an <code>AnyObject</code> instance has a particular
type, you can use the unconditional downcast operator (<code>as!</code>). Performing
an invalid cast triggers a runtime error.</p>

<pre><code class="language-swift">let message = s as! String
print(&quot;Succesful cast to String: \(message)&quot;)
// Prints &quot;Succesful cast to String: This is a bridged string.&quot;

let badCase = v as! String
// Runtime error</code></pre>

<p>Casting is always safe in the context of a <code>switch</code> statement.</p>

<pre><code class="language-swift">let mixedArray: [AnyObject] = [s, v]
for object in mixedArray {
    switch object {
    case let x as String:
        print(&quot;&#39;\(x)&#39; is a String&quot;)
    default:
        print(&quot;&#39;\(object)&#39; is not a String&quot;)
    }
}
// Prints &quot;&#39;This is a bridged string.&#39; is a String&quot;
// Prints &quot;&#39;100&#39; is not a String&quot;</code></pre>

<h1>Accessing Objective-C Methods and Properties</h1>

<p>When you use <code>AnyObject</code> as a concrete type, you have at your disposal
every <code>@objc</code> method and property---that is, methods and properties
imported from Objective-C or marked with the <code>@objc</code> attribute. Because
Swift can&#39;t guarantee at compile time that these methods and properties
are actually available on an <code>AnyObject</code> instance&#39;s underlying type, these
<code>@objc</code> symbols are available as implicitly unwrapped optional methods and
properties, respectively.</p>

<p>This example defines an <code>IntegerRef</code> type with an <code>@objc</code> method named
<code>getIntegerValue()</code>.</p>

<pre><code class="language-swift">class IntegerRef {
    let value: Int
    init(_ value: Int) {
        self.value = value
    }

    @objc func getIntegerValue() -&gt; Int {
        return value
    }
}

func getObject() -&gt; AnyObject {
    return IntegerRef(100)
}

let obj: AnyObject = getObject()</code></pre>

<p>In the example, <code>obj</code> has a static type of <code>AnyObject</code> and a dynamic type
of <code>IntegerRef</code>. You can use optional chaining to call the <code>@objc</code> method
<code>getIntegerValue()</code> on <code>obj</code> safely. If you&#39;re sure of the dynamic type of
<code>obj</code>, you can call <code>getIntegerValue()</code> directly.</p>

<pre><code class="language-swift">let possibleValue = obj.getIntegerValue?()
print(possibleValue)
// Prints &quot;Optional(100)&quot;

let certainValue = obj.getIntegerValue()
print(certainValue)
// Prints &quot;100&quot;</code></pre>

<p>If the dynamic type of <code>obj</code> doesn&#39;t implement a <code>getIntegerValue()</code>
method, the system returns a runtime error when you initialize
<code>certainValue</code>.</p>

<p>Alternatively, if you need to test whether <code>obj.getIntegerValue()</code> exists,
use optional binding before calling the method.</p>

<pre><code class="language-swift">if let f = obj.getIntegerValue {
    print(&quot;The value of &#39;obj&#39; is \(f())&quot;)
} else {
    print(&quot;&#39;obj&#39; does not have a &#39;getIntegerValue()&#39; method&quot;)
}
// Prints &quot;The value of &#39;obj&#39; is 100&quot;</code></pre>

<p><strong>See Also:</strong> <code>AnyClass</code></p>
</div>

<table class="standard">



<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>









